<?php include('includes/startPage.php'); checkLogin(4); ?>
<?php include('process_content_change.php');?>
<?php include('includes/header.php'); printHeader("Browse", "Nordic Sauna");?> <!-- (PageTitle, Banner Text) Prints <head>, opens <body> and prints banner -->
<?php include('includes/navigation.php'); printLeftMenu("browse"); ?> <!-- Navigation include, (currentpagename) -->

<?php OpenContentSection(); ?> 
	<!-- Content Starts -->
	<h2>Content Management</h2>
<?php
        //These forms will only be displayed if you clicked on a software item, actual processing will be done in process_content_change.php

        if (isset($_POST['newSoftware']) && $_POST['newSoftware'] == "Create New Software" && $_SESSION['userLevel'] >= 3)
        {
            //==========================================Create new software==========================================
            include('form_new_software.php');
        }
        else if (isset($_GET['id']) && $_SESSION['userLevel'] >= 3 && isset($_GET['view']) && $_GET['view'] == "manageDescr")
        {
            //==========================================Edit existing software==========================================
            include('form_manage_description.php');
        }
        else if (isset($_GET['id']) && $_SESSION['userLevel'] >= 3 && isset($_GET['view']) && $_GET['view'] == "manageIcon")
        {
            //==========================================Software Avatar / Icon==========================================
            include('form_manage_icon.php');
        }
        else if (isset($_GET['id']) && $_SESSION['userLevel'] >= 3 && isset($_GET['view']) && $_GET['view'] == "manageVersion")
        {
            //==========================================Software Version Management==========================================
            include('form_manage_version.php');        
        }
        else
        {
            //==========================================Else just show the list of software==========================================
?>
            <form name="newSoftware" id="newSoftware" action="manage_content.php" method="POST">
                <input type="submit" name="newSoftware" id="newSoftware" value="Create New Software" />           
                <select name="approvedOrNot" id="approvedOrNot" onchange="location.href='manage_content.php?view=' + this.options[this.selectedIndex].value ">
                <?php
                    if (isset($_GET['view']) && $_GET['view'] == 'approved')
                    {                
                        $softwareList = ns_dao_software::GetApprovedSoftwareList();
                        echo '<option value="approved" selected="selected">Approved</option>';
                        echo '<option value="unapproved">Unapproved</option>';                
                    } else if (isset($_GET['view']) && $_GET['view'] == 'unapproved') {
                        $softwareList = ns_dao_software::GetUnApprovedSoftwareList();
                        echo '<option value="approved">Approved</option>';
                        echo '<option value="unapproved" selected="selected">Unapproved</option>';
                    } else {
                        $softwareList = ns_dao_software::GetApprovedSoftwareList();
                        echo '<option value="approved" selected="selected">Approved</option>';
                        echo '<option value="unapproved">Unapproved</option>';               
                    }
                ?>
                </select>
            </form>
            <br/>           
            
            <table class="displayTable" width="100%" border="1" cellspacing="1" cellpadding="1">
            <tr>
                <td><strong>Software ID</strong></td>
                <td><strong>Software Name</strong></td>
                <td><strong>Description (First 100 chars)</strong></td>
                <td><strong>Category</strong></td>
                <td><strong>Owner</strong></td>
                <td><strong>Approved</strong></td>
                <td><strong>Delete</strong></td>
            </tr>
            
            <?php
                $categoryList = ns_dao_software::GetCategoryList();
                $userList = ns_dao_user::GetUsersList();
                
                foreach ($softwareList as $key => $software)
                {
                    foreach ($categoryList as $key => $category)
                    {
                        if ($software->Category() == $category->ID()) $thisCategory = $category->CategoryDescription();
                    }

                    echo "<tr>";
                        echo "<td>";
                            echo "<a href='manage_content.php?id=" . $software->ID() ."&view=manageDescr'>" . $software->ID() . "</a>";
                        echo "</td>";
                        echo "<td>";
                            echo "<a href='manage_content.php?id=" . $software->ID() ."&view=manageDescr'>" . $software->Name() . "</a>";
                        echo "</td>";
                        echo "<td>";
                            echo "<a href='manage_content.php?id=" . $software->ID() ."&view=manageDescr'>" . substr($software->Description(), 0, 100) . "</a>";
                        echo "</td>";
                        echo "<td>";
                            echo "<a href='manage_content.php?id=" . $software->ID() ."&view=manageDescr'>" . $thisCategory . "</a>";
                        echo "</td>";
                        echo "<td>";
                            echo "<a href='manage_content.php?id=" . $software->ID() ."&view=manageDescr'>" . $software->Owner() . "</a>";
                        echo "</td>";
                        echo "<td>";
                            echo "<a href='manage_content.php?id=" . $software->ID() ."&view=manageDescr'>" . $software->Approved() . "</a>";
                        echo "</td>";
                        echo "<td>";
                            echo '<form name="deleteSoftware" action="manage_content.php" method="post">';
                            echo '<input type="hidden" name="softwareID" id="softwareID" value="'.$software->ID().'"/>';
                            echo '<input type="submit" name="delete" id="delete" value="Delete" onclick="return confirm(\'Are you sure?\');">';
                            echo "</form>";
                        echo "</td>";
                    echo "</tr>";
                }

            ?>
	</table>
	<!-- Content Ends -->
<?php } ?>
<?php CloseContentSection(); ?> 
<?php include('includes/footer.php');?>